import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/components/Login'
import Home from '@/components/Home'
import Welcome from '@/components/Welcome.vue'
import Users from '@/components/sysset/users/Users.vue'
import Setting from '@/components/sysset/setting/Setting.vue'
import Waitting from '@/components/goods/waitting/Waitting.vue'
import Complete from '@/components/goods/complete/Complete.vue'

//报表 echar 
import Report from '@/components/report/Report.vue'

Vue.use(Router)

const router =  new Router({
  routes: [
    {
      //重定向  使得访问地址 http://localhost:8080/#/ 变成 http://localhost:8080/#/login
      path: '/',
      redirect: '/login'
    },
    {
      // 登录页
      path: '/login',
      name: 'Login',
      component: Login
    },
    {
      // 登录页 , 把用户列表，的路由 镶嵌到 主页
      path: '/home',
      name: 'Home',
      component: Home,
      redirect:'/welcome',
      children:[
        { path:"/welcome",component:Welcome },
        { path:"/users",component:Users },
        { path:"/setting",component:Setting },
        { path:"/waitting",component:Waitting },
        { path:"/complete",component:Complete },
        { path:"/report",component:Report }
    ]
    }

  ]
});

//挂在路由导航
router.beforeEach((to,from,next) => {

  //to 将要访问的路径
  //from 从哪个路径来
  // next()  代表放行  next('/login') 强制跳转
  if(to.path==='/login') return next();
  // 不是登陆页，用token验证
  const tokenStr =  window.sessionStorage.getItem("token");
  console.log("路由导航获取的token为："+tokenStr+", 当前访问路径为：" + to.path);
  if(!tokenStr){
    return next('/login');
  }
  next();


})

export default router;



